home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-09-17 | 12.6 KB | 475 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWCmd.cpp
- // Release Version: $ ODF 2 $
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWFrameW.hpp"
-
- #ifndef FWCMD_H
- #include "FWCmd.h"
- #endif
-
- // ----- Framework Includes -----
-
- #ifndef FWPART_H
- #include "FWPart.h"
- #endif
-
- #ifndef FWFRAME_H
- #include "FWFrame.h"
- #endif
-
- #ifndef FWSELECT_H
- #include "FWSelect.h"
- #endif
-
- #ifndef FWPRESEN_H
- #include "FWPresen.h"
- #endif
-
- // ----- OS Layer -----
-
- #ifndef FWMENUS_K
- #include "FWMenus.k"
- #endif
-
- #ifndef FWRESACC_H
- #include "FWResAcc.h"
- #endif
-
- #ifndef FWRESTYP_H
- #include "FWResTyp.h"
- #endif
-
- #ifndef SLODFSTR_K
- #include "SLODFStr.k"
- #endif
-
- #ifndef SLODFSTR_H
- #include "SLODFStr.h"
- #endif
-
- #ifndef FWSESION_H
- #include "FWSesion.h"
- #endif
-
- // ----- Foundation Layer -----
-
- #ifndef FWCFMRES_H
- #include "FWCFMRes.h"
- #endif
-
- #ifndef FWMEMMGR_H
- #include "FWMemMgr.h"
- #endif
-
- #ifndef FWSTRS_H
- #include "FWStrs.h"
- #endif
-
- #ifndef FWBNDSTR_H
- #include "FWBndStr.h"
- #endif
-
- // ----- OpenDoc Includes -----
-
- // for ODIText
- #ifndef _ITEXT_
- #include "IText.h"
- #endif
-
- #ifndef SOM_Module_OpenDoc_Commands_defined
- #include <CmdDefs.xh>
- #endif
-
- #ifndef SOM_ODPart_xh
- #include "Part.xh"
- #endif
-
- #ifndef SOM_ODFrame_xh
- #include "Frame.xh"
- #endif
-
- #ifndef SOM_ODStorageUnit_xh
- #include <StorageU.xh>
- #endif
-
- // ----- Platform Includes -----
-
- #if defined(FW_BUILD_MAC) && !defined(__SCRIPT__)
- #include <Script.h>
- #endif
-
- #if defined(FW_BUILD_WIN32S) && !defined(__WINNT_H)
- #include <winnt.h>
- #endif
-
-
- //========================================================================================
- // Runtime Info
- //========================================================================================
-
- #ifdef FW_BUILD_MAC
- #pragma segment odfcommands
- #endif
-
- FW_DEFINE_AUTO(FW_CCommand)
-
- //========================================================================================
- // FW_CCommand class
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand constructor
- //----------------------------------------------------------------------------------------
-
- FW_CCommand::FW_CCommand(Environment* ev,
- ODCommandID id,
- FW_CFrame* frame,
- FW_Boolean canUndo) :
- fCommandID(id),
- fCanUndo(canUndo),
- fCausesChange(TRUE),
- fActionType(kODSingleAction),
- fPart(NULL),
- fFrame(frame),
- fUndoString(NULL),
- fRedoString(NULL),
- fHasAddedAction(false)
- {
- FW_ASSERT(fFrame);
-
- fPart = fFrame->GetPart(ev);
- fPart->GetODPart(ev)->Acquire(ev);
-
- // [HLX] ATTENTION: Because secondary frames may have disappeared during Undo
- // we need to use the source frame
- fSourceFrame = fFrame->GetSourceFrame(ev);
-
- fPresentation = fFrame->GetPresentation(ev);
-
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand destructor
- //----------------------------------------------------------------------------------------
-
- FW_CCommand::~FW_CCommand()
- {
- FW_START_DESTRUCTOR
-
- FW_SOMEnvironment ev;
- fPart->GetODPart(ev)->Release(ev);
-
- PrivDisposeUndoStrings();
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::PrivDisposeUndoStrings
- //----------------------------------------------------------------------------------------
-
- void FW_CCommand::PrivDisposeUndoStrings()
- {
- if (fUndoString)
- ::DisposeIText(fUndoString);
- if (fRedoString)
- ::DisposeIText(fRedoString);
-
- fUndoString = fRedoString = NULL;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::Execute
- //----------------------------------------------------------------------------------------
- // Note: Execute returns true if the command is on the undo stack. It returns false if the
- // command has been deleted because it was not undoable
-
- FW_Boolean FW_CCommand::Execute(Environment* ev, FW_Boolean deleteIfNotUndoable)
- {
- FW_Boolean wasDone = FALSE;
- FW_Boolean isOnUndoStack = TRUE;
- FW_VOLATILE(wasDone);
-
- FW_TRY
- {
- this->DoIt(ev);
- wasDone = TRUE;
-
- if (GetCausesChange(ev))
- PropagateChanges(ev);
-
- if (GetCanUndo(ev))
- AddAction(ev, fActionType, (octet*) &this, sizeof(FW_CCommand*), GetUndoString(ev), GetRedoString(ev));
- }
- FW_CATCH_BEGIN
- FW_CATCH_EVERYTHING()
- {
- if (!wasDone)
- {
- CommitDone(ev);
- delete this;
- FW_THROW_SAME();
- }
-
- // Don't rethrow if the command was completed
- SetCanUndo(ev, FALSE);
- }
- FW_CATCH_END
-
- // If the command has not been added to the action stack then kill it
-
- if (!HasAddedAction(ev))
- {
- CommitDone(ev);
- if (deleteIfNotUndoable)
- delete this;
- isOnUndoStack = FALSE;
- }
-
- return isOnUndoStack;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::UndoIt
- //----------------------------------------------------------------------------------------
-
- void FW_CCommand::UndoIt(Environment* ev)
- {
- FW_UNUSED(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::RedoIt
- //----------------------------------------------------------------------------------------
-
- void FW_CCommand::RedoIt(Environment* ev)
- {
- FW_UNUSED(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::CommitDone
- //----------------------------------------------------------------------------------------
-
- void FW_CCommand::CommitDone(Environment* ev)
- {
- // Called by FW_CPart::DisposeActionState before deleting the command
- // The command has been done, and possibly redone
-
- if (GetCanUndo(ev))
- this->FreeUndoState(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::CommitUndone
- //----------------------------------------------------------------------------------------
-
- void FW_CCommand::CommitUndone(Environment* ev)
- {
- // Called by FW_CPart::DisposeActionState before deleting the command
- // The last action for this command was an Undo
-
- if (GetCanUndo(ev))
- this->FreeRedoState(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::SaveUndoState
- //----------------------------------------------------------------------------------------
- void FW_CCommand::SaveUndoState(Environment* ev)
- {
- FW_UNUSED(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::SaveRedoState
- //----------------------------------------------------------------------------------------
- void FW_CCommand::SaveRedoState(Environment* ev)
- {
- FW_UNUSED(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::FreeUndoState
- //----------------------------------------------------------------------------------------
- void FW_CCommand::FreeUndoState(Environment* ev)
- {
- FW_UNUSED(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::FreeRedoState
- //----------------------------------------------------------------------------------------
- void FW_CCommand::FreeRedoState(Environment* ev)
- {
- FW_UNUSED(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::SetMenuStrings
- //----------------------------------------------------------------------------------------
-
- void FW_CCommand::SetMenuStrings(Environment* ev,
- const FW_CString& undoString,
- const FW_CString& redoString)
- {
- FW_UNUSED(ev);
- PrivDisposeUndoStrings();
- fUndoString = ::CopyIText(undoString.RevealODIText());
- fRedoString = ::CopyIText(redoString.RevealODIText());
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::SetMenuStrings
- //----------------------------------------------------------------------------------------
-
- void FW_CCommand::SetMenuStrings(Environment* ev,
- char* undoChars,
- char* redoChars)
- {
- FW_CString255 undoString(undoChars);
- FW_CString255 redoString(redoChars);
- this->SetMenuStrings(ev, undoString, redoString);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::SetMenuStringsFromResource
- //----------------------------------------------------------------------------------------
- void FW_CCommand::SetMenuStringsFromResource(Environment* ev,
- short stringResourceID,
- short undoStringID,
- short redoStringID)
- {
- FW_PSharedLibraryResourceFile resFile(ev);
- FW_CString undoString;
- FW_CString redoString;
- ::FW_LoadStringByID(ev, resFile, stringResourceID, FW_kMultiStringRes, undoStringID, undoString);
- ::FW_LoadStringByID(ev, resFile, stringResourceID, FW_kMultiStringRes, redoStringID, redoString);
- this->SetMenuStrings(ev, undoString, redoString);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::AddAction
- //----------------------------------------------------------------------------------------
-
- void FW_CCommand::AddAction(Environment* ev,
- ODActionType actionType,
- octet* dataPtr,
- unsigned long dataSize,
- ODName* undoActionLabel,
- ODName* redoActionLabel)
- {
- // Set up the action data record
- ODActionData actionState;
- actionState._maximum = dataSize;
- actionState._length = dataSize;
- actionState._buffer = dataPtr;
-
- FW_CSession::GetUndo(ev)->AddActionToHistory(ev, fPart->GetODPart(ev),
- &actionState,
- actionType,
- undoActionLabel,
- redoActionLabel);
- if (dataSize != 0)
- fHasAddedAction = TRUE; // OpenDoc has a ptr to this command object
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::IsOKtoEdit
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CCommand::IsOKtoEdit(Environment* ev)
- {
- FW_Boolean result = true;
-
- if (fCausesChange)
- {
- if (fPart->IsReadOnly(ev))
- result = false;
- else
- result = !fFrame->IsFrameInLinkDestination(ev);
- }
-
- return result;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::PrivLoadDefaultUndoStrings
- //----------------------------------------------------------------------------------------
-
- void FW_CCommand::PrivLoadDefaultUndoStrings(Environment* ev)
- {
- FW_CString undoString;
- FW_CString redoString;
-
- ::FW_PrivLoadDefaultUndoStrings(ev, undoString, redoString);
- SetMenuStrings(ev, undoString, redoString);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::AddEndAction (static)
- //----------------------------------------------------------------------------------------
-
- void FW_CCommand::AddEndAction(Environment* ev, short undoMsgID, FW_CPart* part)
- {
- FW_CString undoString;
- FW_CString redoString;
- ::FW_PrivLoadUndoStrings(ev, undoMsgID, undoString, redoString);
- ODName* undoActionLabel = ::CopyIText(undoString.RevealODIText());
- ODName* redoActionLabel = ::CopyIText(redoString.RevealODIText());
-
- // Set up an empty action data record
- ODActionData actionState;
- actionState._maximum = 0;
- actionState._length = 0;
- actionState._buffer = NULL;
-
- FW_CSession::GetUndo(ev)->AddActionToHistory(ev, part->GetODPart(ev),
- &actionState,
- kODEndAction,
- undoActionLabel,
- redoActionLabel);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::HandleUndo (static)
- //----------------------------------------------------------------------------------------
-
- void FW_CCommand::HandleUndo(Environment* ev)
- {
- UndoIt(ev);
-
- if (GetCausesChange(ev))
- PropagateChanges(ev);
-
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::HandleRedo (static)
- //----------------------------------------------------------------------------------------
-
- void FW_CCommand::HandleRedo(Environment* ev)
- {
- RedoIt(ev);
-
- if (GetCausesChange(ev))
- PropagateChanges(ev);
-
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CCommand::PropagateChanges (static)
- //----------------------------------------------------------------------------------------
-
- void FW_CCommand::PropagateChanges(Environment* ev, ODUpdateID id)
- {
- GetPresentation(ev)->ContentUpdated(ev, id);
- fPart->Changed(ev);
- }
-
-